<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------

namespace app\model\user;

use crmeb\basic\BaseModel;
use crmeb\traits\ModelTrait;
use think\model;
use think\facade\Db;

/**
 * Class UserAddress
 * @package app\model\user
 */
class UserAddress extends BaseModel
{
    use ModelTrait;

    /**
     * 数据表主键
     * @var string
     */
    protected $pk = 'id';

    /**
     * 模型名称
     * @var string
     */
    protected $name = 'user_address';

    protected $insert = ['add_time'];

    protected $hidden = ['add_time', 'is_del'];

    /**
     * 更新之后事件
     * @param model $model
     */
    public static function onAfterUpdate(Model $model)
    {
        event('user.address.update');
    }

    protected function setAddTimeAttr()
    {
        return time();
    }

    /**
     * 用户uid
     * @param $query
     * @param $value
     */
    public function searchUidAttr($query, $value)
    {
        $query->where('uid', $value);
    }

    /**
     * 是否删除
     * @param Model $query
     * @param $value
     */
    public function searchIsDelAttr($query, $value)
    {
        $query->where('is_del', $value);
    }

    /**
     * 是否默认地址
     * @param Model $query
     * @param $value
     */
    public function searchIsDefaultAttr($query, $value)
    {
        $query->where('is_default', $value);
    }


    public static function syncUserAddress($addressInfo)
    {
        $add = [
            "address_id" => $addressInfo['address_id'] ?? '',
            "uid" => $addressInfo['uid'] ?? '',
            "real_name" => $addressInfo['real_name'] ?? '',
            "phone" => $addressInfo['phone'] ?? '',
            "province" => $addressInfo['province'] ?? '',
            "province_id" => $addressInfo['province_id'] ?? 1,
            "city" => $addressInfo['city'] ?? '',
            "city_id" => $addressInfo['city_id'] ?? '',
            "district" => $addressInfo['district'] ?? '',
            "district_id" => $addressInfo['district_id'] ?? '',
            "street" => $addressInfo['street'] ?? '',
            "street_id" => $addressInfo['street_id'] ?? '',
            "detail" => $addressInfo['detail'] ?? '',
            "post_code" => $addressInfo['post_code'] ?? '',
            "longitude" => $addressInfo['longitude'] ?? '',
            "latitude" => $addressInfo['latitude'] ?? '',
            "is_default" => $addressInfo['is_default'] ?? '',
            "create_time" => date("Y-m-d H:i:s"),
        ];
//        return Db::connect('mysql2')->table('eb_user_address')->insert($add);
    }




    public static function syncUpdateUserAddress($id, $addressInfo)
    {
        if(!empty($addressInfo["uid"])) $update["uid"] = $addressInfo["uid"];
        if(!empty($addressInfo["real_name"])) $update["real_name"] = $addressInfo["real_name"];
        if(!empty($addressInfo["phone"])) $update["phone"] = $addressInfo["phone"];
        if(!empty($addressInfo["province"])) $update["province"] = $addressInfo["province"];
        if(!empty($addressInfo["province_id"])) $update["province_id"] = $addressInfo["province_id"];
        if(!empty($addressInfo["city"])) $update["city"] = $addressInfo["city"];
        if(!empty($addressInfo["city_id"])) $update["city_id"] = $addressInfo["city_id"];
        if(!empty($addressInfo["district"])) $update["district"] = $addressInfo["district"];
        if(!empty($addressInfo["district_id"])) $update["district_id"] = $addressInfo["district_id"];
        if(!empty($addressInfo["street"])) $update["street"] = $addressInfo["street"];
        if(!empty($addressInfo["street_id"])) $update["street_id"] = $addressInfo["street_id"];
        if(!empty($addressInfo["detail"])) $update["detail"] = $addressInfo["detail"];
        if(!empty($addressInfo["post_code"])) $update["post_code"] = $addressInfo["post_code"];
        if(!empty($addressInfo["longitude"])) $update["longitude"] = $addressInfo["longitude"];
        if(!empty($addressInfo["latitude"])) $update["latitude"] = $addressInfo["latitude"];
        if(!empty($addressInfo["is_default"])) $update["is_default"] = $addressInfo["is_default"];

//        return Db::connect('mysql2')->table('eb_user_address')->where("address_id", $id)->update($update);
    }





}
